// --------------------------------
// <copyright file="TableColumnCollection.cs" company="TRONSoft">
//     Copyright (c) TRONSoft.
// </copyright>
// <license>
//   This source code is subject to terms and conditions of the Microsoft
//   Public License. A copy of the license can be found in the License.html
//   file at the root of this distribution. If you cannot locate the
//   Microsoft Public License, please send an email to dlr@microsoft.com.
//   By using this source code in any fashion, you are agreeing to be bound 
//   by the terms of the Microsoft Public License. You must not remove this
//   notice, or any other, from this software.
// </license>
// <author>Tron</author>
// ---------------------------------

using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;

namespace Tronsoft.Actucal.Tables
{
    public class TableColumnCollection : InternalDataCollectionBase
    {
        private readonly ArrayList columns = new ArrayList();
        private readonly Dictionary<string, TableColumn> columnsFromName = new Dictionary<string, TableColumn>();
        private readonly DataColumnCollection dataColumns;

        internal TableColumnCollection(DataColumnCollection dataColumns)
        {
            if (dataColumns == null) throw new ArgumentNullException("dataColumns");

            this.dataColumns = dataColumns;

            foreach (DataColumn column in dataColumns)
            {
                TableColumn tableColumn = new TableColumn(column.ColumnName, column.DataType);
                columns.Add(tableColumn);
                columnsFromName.Add(column.ColumnName, tableColumn);
            }
        }

        public TableColumn this[string name]
        {
            get
            {
                foreach (KeyValuePair<string, TableColumn> pair in columnsFromName)
                {
                    if (pair.Key.ToLowerInvariant() == name.ToLowerInvariant())
                    {
                        return pair.Value;
                    }
                }

                return null;
            }
        }

        public TableColumn this[int index]
        {
            get { return columns[index] as TableColumn; }
        }

        protected override ArrayList List
        {
            get { return columns; }
        }

        public void Add(TableColumn column)
        {
            if (column == null)
                return;

            columns.Add(column);
            columnsFromName.Add(column.ColumnName, column);
            dataColumns.Add(new DataColumn(column.ColumnName, column.DataType));
        }

        public TableColumn Add(string columnName, Type dataType)
        {
            if (dataType == null) throw new ArgumentNullException("dataType");
            if (string.IsNullOrEmpty(columnName)) throw new ArgumentNullException();

            if (Contains(columnName))
            {
                return columnsFromName[columnName];
            }
            else
            {
                TableColumn column = new TableColumn(columnName, dataType);
                Add(column);

                return column; 
            }
        }

        public bool Contains(string columnName)
        {
            return columnsFromName.ContainsKey(columnName);
        }
    }
}